#ifndef _M_UTIL_H_
#define _M_UTIL_H_


#include<iostream>
#include<ctime>
#include<sys/stat.h>//stat 检查文件是否存在的头文件
namespace log
{
    namespace util
    {
        class Date //获取当前时间
        {
            public:
            static const time_t now()
            {
                return((const time_t)time(nullptr));

            }
        };


        class File
        {
            public:
          
            static bool Exists(const std::string &pathname)
            {
                struct stat st;
                if(stat(pathname.c_str(), &st)< 0)// 
                {
                    return false;
                }
                return true;
            }
            static std::string Path(const std::string &pathname)//获取副级目录 ./abc/fgdg/abc.txt
            {
                size_t pos = pathname.find_last_of("/\\");  //(查找斜杠和反斜杠"/ \\" 是为了适应Linux和win两个平台)
                if(pos == std::string::npos) return ".";
                return pathname.substr(0,pos+1);//连'/'也截取   
            }

            static void CreateDirectory(const std::string &pathname)
            {
                size_t pos = 0, idx = 0;
                while(idx < pathname.size())
                {
                    pos = pathname.find_first_of("/\\",idx);//每次从idx位置开始查找
                    if(pos == std::string::npos)
                     mkdir(pathname.c_str(), 0777);

                     std::string parent_dir = pathname.substr(0, pos+1);
                    if(Exists(parent_dir) == true)
                    {
                        idx = pos+1;
                        continue;
                    }
                    mkdir(parent_dir.c_str(), 0777);
                    idx=pos+1;
                }
            }
        };
    }
}
#endif